package com.dyz.LeetCodeforSwordFingerOffer;

import java.util.HashMap;

/**
 * @author: daiyizheng
 * @date: 2021/7/6 19:57
 * @description:
 */
public class CopyRandomList35 {
    //HashMap方式
    public Node copyRandomList(Node head) {
        if(head==null){
            return head;
        }
        Node cur = head;
        HashMap<Node,Node> map = new HashMap<>();
        //第一次遍历，存val
        while (cur!=null){
            map.put(cur, new Node(cur.val));
            cur = cur.next;
        }
        //第二次遍历，处理next和random
        cur = head;
        while (cur!=null){
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        return map.get(head);
    }
}
